package lxt.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import lxt.action.core.ActionSupport;
import lxt.action.core.DateUtil;
import lxt.entity.CostAccident;
import lxt.entity.DriverInfo;
import lxt.entity.InvoiceOrder;
import lxt.entity.TruckInfo;
import lxt.entity.UserInfo;
import lxt.service.CostAccidentService;
import lxt.service.DriverService;
import lxt.service.InvoiceOrderService;
import lxt.service.TruckService;
/**
 * 交通事故管理action
 * @author Lit
 *
 */
public class CostAccidentAction extends ActionSupport{
	
	private static final long serialVersionUID = 1L;
	//定义属性
	private String id;
	private String truckId;
	private String orderId;
	private String driverId;
	private String location;
	private String losses;
	private Integer toll;
	private String liability;
	private String fineCharge;
	private String claimFee;
	private String accidentTime;
	private String note;
	private String entryTime;
	private String entryUser;
    //定义service
    private CostAccidentService costAccidentService;
    private DriverService driverService ; //用户获取驾驶员
    private TruckService truckService ; //用于获取车辆
    private InvoiceOrderService invoiceOrderService ;//用于获取订单号
    //定义对象
	private List<CostAccident> costAccidents;
	private List<TruckInfo> trucks; //车牌号list
	private List<InvoiceOrder> invoiceOrders ; //订单号list
	private List<DriverInfo> drivers ; //驾驶员list
	
	private String deleteIds;
	//定义分页变量
	private Integer pageCounts = 10; //每页显示数量
	private String currPageStr = "1"; //分页
	private Integer totalCounts ;//总数据量
	/**
	 * 加载
	 * @return
	 */
	public String loadAll(){
		try{
			//定义从第几条显示
			int offset =( Integer.parseInt(currPageStr)-1)*pageCounts;
			//获得交通事故列表
			costAccidents = costAccidentService.loadCostAccident(truckId, driverId, offset, pageCounts);
			//获取总的记录数
			totalCounts = costAccidentService.loadAllCostAccidentCount(truckId, driverId);
		}catch(Exception ex){
			ex.printStackTrace();
			this.addActionMessage("操作失败，请检查重新操作");
		}
		return SUCCESS;
	}
	/**
	 * 添加预处理
	 * @return
	 */
	public String AddPre(){
		try{
			trucks = truckService.loadAll();
			drivers = driverService.loadAll();
			invoiceOrders = invoiceOrderService.loadAll();
		}catch(Exception ex){
			ex.printStackTrace();
			this.addActionMessage("操作失败，请检查重新操作");
		}
		return SUCCESS;
	}
	/**
	 * 添加
	 * @return
	 */
	public String add(){
		try{
			//创建新的对象
			CostAccident costAccident = new CostAccident();
			//为对象赋值
			if (accidentTime != null && !accidentTime.equals("")) {
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				Date time = sdf.parse(accidentTime);
				costAccident.setAccidentTime(time);
			}
			if(claimFee!=null&&!claimFee.equals("")){
				costAccident.setClaimFee(Double.parseDouble(claimFee));
			}
			costAccident.setDriverId(driverId);
			UserInfo user = (UserInfo) this.getSession().get("user"); //获取操作员
			costAccident.setEntryUser(user.getUsername());
			if(fineCharge!=null&&!fineCharge.equals("")){
				costAccident.setFineCharge(Double.parseDouble(fineCharge));
			}
			costAccident.setLiability(liability);
			costAccident.setLocation(location);
			if(losses!=null&&!losses.equals("")){
				costAccident.setLosses(Double.parseDouble(losses));
			}
			costAccident.setNote(note);
			costAccident.setOrderId(orderId);
			costAccident.setToll(toll);
			costAccident.setTruckId(truckId);
			this.costAccidentService.addCostAccident(costAccident);
		}catch(Exception ex){
			ex.printStackTrace();
			this.addActionMessage("操作失败，请检查重新操作");
		}
		return SUCCESS;
	}
	/**
	 * 编辑预处理
	 * @return
	 */
	public String updatePre(){
		try{
			trucks = truckService.loadAll();
			drivers = driverService.loadAll();
			invoiceOrders = invoiceOrderService.loadAll();
			//获得参数
			CostAccident costAccident = this.costAccidentService.loadCostAccidentById(id);
			/**
			 * 查看用户操作时间 设置编辑权限
			 */
			UserInfo user = (UserInfo) this.getSession().get("user");
			if(user.getLoginType()!=0){//记录员
				Date preTime = costAccident.getEntryTime();
				int falg = DateUtil.getDays(preTime);
				if(falg!=0){
					this.addActionMessage("无权修改数据,请联系管理员");
					this.loadAll();
					return INPUT;
				}
			}
			//为页面属性赋值
			this.accidentTime = costAccident.getAccidentTime().toString();
			this.claimFee = String.valueOf(costAccident.getClaimFee());
			this.driverId = costAccident.getDriverId();
			//this.entryTime = costAccident.getEntryTime();
			this.fineCharge = String.valueOf(costAccident.getFineCharge());
			this.id = costAccident.getId();
			this.liability = costAccident.getLiability();
			this.location = costAccident.getLocation();
			this.losses = String.valueOf(costAccident.getLosses());
			this.note = costAccident.getNote();
			this.orderId = costAccident.getOrderId();
			this.toll = costAccident.getToll();
			this.truckId = costAccident.getTruckId();
		}catch(Exception ex){
			ex.printStackTrace();
			this.addActionMessage("操作失败，请检查重新操作");
		}
		return SUCCESS;
	}
	/**
	 * 修改
	 * @return
	 */
	public String update(){
		try{
			//重新获取修改目标
			CostAccident costAccident  = this.costAccidentService.loadCostAccidentById(id);
			//更新目标对象的目标修改属性(部分属性)
			//costAccident.setAccidentTime(accidentTime);
			if(claimFee!=null&&!claimFee.equals("")){
				costAccident.setClaimFee(Double.parseDouble(claimFee));
			}
			costAccident.setDriverId(driverId);
			if(fineCharge!=null&&!fineCharge.equals("")){
				costAccident.setFineCharge(Double.parseDouble(fineCharge));
			}
			costAccident.setLiability(liability);
			costAccident.setLocation(location);
			if(losses!=null&&!losses.equals("")){
				costAccident.setLosses(Double.parseDouble(losses));
			}
			costAccident.setNote(note);
			costAccident.setOrderId(orderId);
			costAccident.setToll(toll);
			costAccident.setTruckId(truckId);
			this.costAccidentService.updateCostAccident(costAccident);
		}catch(Exception ex){
			ex.printStackTrace();
			this.addActionMessage("操作失败，请检查重新操作");
		}
		return SUCCESS;
	}
	/**
	 * 删除
	 * @return
	 */
	public String delete(){
		try{
			/**
			 * 查看用户操作时间 设置编辑权限
			 */
			UserInfo user = (UserInfo) this.getSession().get("user");
			if(user.getLoginType()!=0){//记录员
				for(int i=0;i<deleteIds.length();i++){
					String[] delId = deleteIds.split(",");
					CostAccident c = this.costAccidentService.loadCostAccidentById(delId[i]);
					int falg = DateUtil.getDays(c.getEntryTime());
					if(falg!=0){
						this.addActionMessage("无权修改数据,请联系管理员");
						this.loadAll();
						return INPUT;
					}
				}
			}
			this.costAccidentService.deleteCostAccident(deleteIds);
		}catch(Exception ex){
			ex.printStackTrace();
			this.addActionMessage("操作失败，请检查重新操作");
		}
		return SUCCESS;
	}
	
	/**
	 * get set
	 */
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getTruckId() {
		return truckId;
	}
	public void setTruckId(String truckId) {
		this.truckId = truckId;
	}
	public String getOrderId() {
		return orderId;
	}
	public void setOrderId(String orderId) {
		this.orderId = orderId;
	}
	public String getDriverId() {
		return driverId;
	}
	public void setDriverId(String driverId) {
		this.driverId = driverId;
	}
	public String getLocation() {
		return location;
	}
	public void setLocation(String location) {
		this.location = location;
	}
	public Integer getToll() {
		return toll;
	}
	public void setToll(Integer toll) {
		this.toll = toll;
	}
	public String getLiability() {
		return liability;
	}
	public void setLiability(String liability) {
		this.liability = liability;
	}
	public String getLosses() {
		return losses;
	}
	public void setLosses(String losses) {
		this.losses = losses;
	}
	public String getFineCharge() {
		return fineCharge;
	}
	public void setFineCharge(String fineCharge) {
		this.fineCharge = fineCharge;
	}
	public String getClaimFee() {
		return claimFee;
	}
	public void setClaimFee(String claimFee) {
		this.claimFee = claimFee;
	}
	public String getAccidentTime() {
		return accidentTime.toString();
	}
	public void setAccidentTime(String accidentTime) {
		this.accidentTime = accidentTime;
	}
	public String getNote() {
		return note;
	}
	public void setNote(String note) {
		this.note = note;
	}
	public String getEntryTime() {
		return entryTime;
	}
	public void setEntryTime(String entryTime) {
		this.entryTime = entryTime;
	}
	public String getEntryUser() {
		return entryUser;
	}
	public void setEntryUser(String entryUser) {
		this.entryUser = entryUser;
	}
	public CostAccidentService getCostAccidentService() {
		return costAccidentService;
	}
	public void setCostAccidentService(CostAccidentService costAccidentService) {
		this.costAccidentService = costAccidentService;
	}
	public DriverService getDriverService() {
		return driverService;
	}
	public void setDriverService(DriverService driverService) {
		this.driverService = driverService;
	}
	public TruckService getTruckService() {
		return truckService;
	}
	public void setTruckService(TruckService truckService) {
		this.truckService = truckService;
	}
	public List<CostAccident> getCostAccidents() {
		return costAccidents;
	}
	public void setCostAccidents(List<CostAccident> costAccidents) {
		this.costAccidents = costAccidents;
	}
	public List<TruckInfo> getTrucks() {
		return trucks;
	}
	public void setTrucks(List<TruckInfo> trucks) {
		this.trucks = trucks;
	}
	public String getDeleteIds() {
		return deleteIds;
	}
	public void setDeleteIds(String deleteIds) {
		this.deleteIds = deleteIds;
	}
	public Integer getPageCounts() {
		return pageCounts;
	}
	public void setPageCounts(Integer pageCounts) {
		this.pageCounts = pageCounts;
	}
	public String getCurrPageStr() {
		return currPageStr;
	}
	public void setCurrPageStr(String currPageStr) {
		this.currPageStr = currPageStr;
	}
	public Integer getTotalCounts() {
		return totalCounts;
	}
	public void setTotalCounts(Integer totalCounts) {
		this.totalCounts = totalCounts;
	}
	public InvoiceOrderService getInvoiceOrderService() {
		return invoiceOrderService;
	}
	public void setInvoiceOrderService(InvoiceOrderService invoiceOrderService) {
		this.invoiceOrderService = invoiceOrderService;
	}
	public List<InvoiceOrder> getInvoiceOrders() {
		return invoiceOrders;
	}
	public void setInvoiceOrders(List<InvoiceOrder> invoiceOrders) {
		this.invoiceOrders = invoiceOrders;
	}
	public List<DriverInfo> getDrivers() {
		return drivers;
	}
	public void setDrivers(List<DriverInfo> drivers) {
		this.drivers = drivers;
	}
	
}
